package com.wenbing.miio.core;

import com.wenbing.miio.core.exception.NetworkLayerException;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 *
 * @author wen bing
 * @projectName: miio
 * @package: com.wenbing.miio.core
 * @className: AbstractNetworkStackFrame
 * @date 2024/09/19 14:19
 *
 *  共享上层Context的网络层，定义处理流程
 *  可作为上层的附属层
 *  入栈
 *       解码 -> 交付下层处理
 *  出栈
 *       编码 -> 交付上层处理
 **/
public abstract class AbstractShareContextNetworkLayer extends AbstractNetworkLayer {

    @Override
    public void inbound(NetworkLayerContext context, ByteBuffer packingData) throws IOException, NetworkLayerException {
        packingData = unpacking(context, packingData);
        log("inbound --> share", context, packingData);
        // 交由下层协议处理
        deliverLowerLayer(context, packingData);
    }

    @Override
    public void outbound(NetworkLayerContext context, ByteBuffer unpackingData) throws IOException, NetworkLayerException {
        unpackingData = packing(context, unpackingData);
        log("outbound --> share", context, unpackingData);
        // 交由上层协议处理
        deliverUpperLayer(context, unpackingData);
    }
}
